package com.itfreer.bpm.flow.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.itfreer.bpm.flow.api.IBaseWorkFlowService;
import com.itfreer.bpm.flow.api.IBpmProjectEntity;
import com.itfreer.bpm.flow.api.IBpmWorkFlowService;
import com.itfreer.bpm.flow.api.IProofReadUser;
import com.itfreer.bpm.flow.findwork.para.FindWorkInfo;
import com.itfreer.bpm.history.entity.BpmHistoryEntity;
import com.itfreer.bpm.messager.BpmMessage;
import com.itfreer.bpm.option.para.ObservationInfo;
import com.itfreer.bpm.option.para.OptionInfo;
import com.itfreer.bpm.option.para.ProjActCommonInfo;
import com.itfreer.bpm.power.para.UserInfo;
import com.itfreer.form.api.imp.DictionaryConvertServiceImp;

/**
 * 业务逻辑的实现逻辑基础类
 * @author gj
 *
 * @param <T>
 */
public abstract class BaseWorkFlowServiceImp<T extends IBpmProjectEntity> extends DictionaryConvertServiceImp<T> implements IBaseWorkFlowService<T>{
	/**
	 * 流程驱动服务类
	 */
	@Autowired
	private IBpmWorkFlowService<T> bpmFlowService;
	
	@Autowired(required=false)
	private IProofReadUser proofRead;
	
	/**
	 * 获取工作记录
	 * @param bpmKey 流程主键
	 * @param 项目id 项目id
	 * @param user 用户信息
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public FindWorkInfo<T> findWork(String bpmKey,String id,UserInfo user){
		//用户校验
		if(proofRead!=null) {
			proofRead.ProofReadUser(user);
		}
		FindWorkInfo info=new FindWorkInfo();
		info.setBpmKey(bpmKey);
		info.setId(id);
		if(id!=null && !"".equals(id)) {
			//T entity=.getEntity(id);
			T entity=super.getEntity(id);
			if(entity!=null) {
				info.setEntity(entity);
				info.setSexeid(entity.getSexeid());
			}
		}
		this.getBpmWorkFlowService().findWork(info, user);
		return info;
	}

	@Override
	public OptionInfo getNextTask(String definedid, T entity,List<BpmHistoryEntity> editHistory,UserInfo user) {
		//用户校验
		if(proofRead!=null) {
			proofRead.ProofReadUser(user);
		}
		
		OptionInfo info= bpmFlowService.getNextTask(definedid, entity,editHistory, user);
		//提交到下一个节点时，当前节点数据
		if(entity!=null) {
			String id=entity.getId();
			//T sjkEntity=this.getDao().getEntity(id);
			T sjkEntity=super.getEntity(id);
			if(sjkEntity==null) {
				//this.getDao().add(entity);
				super.add(entity);
			}else {
				//this.getDao().update(entity);
				super.update(entity);
			}
			info.setEntity(entity);
		}
		return info;
	}
	
	/**
	 * 项目提交操作
	 * @param oInfo 操作信息
	 * @param entity 项目实体
	 * @param user 用户
	 * @return
	 */
	@Override
	public BpmMessage signTask(OptionInfo oInfo,T entity,UserInfo user) {
		//用户校验
		if(proofRead!=null) {
			proofRead.ProofReadUser(user);
		}
		BpmMessage message= bpmFlowService.signTask(oInfo, entity, user);
		super.update(entity);
		message.setEntity(entity);
		return message;
		
	}
	
	/**
	 * 获取退回节点
	 */
	@Override
	public OptionInfo getBackTask(String definedid, T entity,List<BpmHistoryEntity> editHistory,UserInfo user) {
		//用户校验
		if(proofRead!=null) {
			proofRead.ProofReadUser(user);
		}
		//读取退回节点信息
		OptionInfo info= bpmFlowService.getBackTask(definedid, entity,editHistory, user);
		//提交到下一个节点时，当前节点数据
		if(entity!=null) {
			String id=entity.getId();
			//T sjkEntity=this.getDao().getEntity(id);
			T sjkEntity=super.getEntity(id);
			if(sjkEntity==null) {
				//this.getDao().add(entity);
				super.add(entity);
			}else {
				//this.getDao().update(entity);
				super.update(entity);
			}
			info.setEntity(entity);
		}
		return info;
	}

	@Override
	public BpmMessage sendBack(OptionInfo optInfo,T entity,UserInfo user) {
		
		//用户校验
		if(proofRead!=null) {
			proofRead.ProofReadUser(user);
		}
		BpmMessage message= bpmFlowService.sendBaskTask(optInfo, entity, user);
		super.update(entity);
		message.setEntity(entity);
		return message;
		//return null;
	}

	@Override
	public void retractTask(UserInfo user, String sexeid) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void destoryAct(ObservationInfo info, String sexeid) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void rebirthAct(ObservationInfo info, String sexeid) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void suspendAct(ObservationInfo info, String sexeid) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void activateAct(ObservationInfo info, String sexeid) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public ProjActCommonInfo getActCommonInfo(String sexeid) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 获取流程驱动服务类
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	private IBpmWorkFlowService getBpmWorkFlowService() {
		return this.bpmFlowService;
	}
	
	/**
	 * 实现流程撤回功能
	 */
	@Override
	public OptionInfo getSkipTask(String definedid, T entity,List<BpmHistoryEntity> editHistory,UserInfo user) {
		//用户校验
		if(proofRead!=null) {
			proofRead.ProofReadUser(user);
		}
		
		OptionInfo info= bpmFlowService.getSkipTask(definedid, entity,editHistory, user);
		//提交到下一个节点时，当前节点数据
		if(entity!=null) {
			String id=entity.getId();
			//T sjkEntity=this.getDao().getEntity(id);
			T sjkEntity=super.getEntity(id);
			if(sjkEntity==null) {
				//this.getDao().add(entity);
				super.add(entity);
			}else {
				//this.getDao().update(entity);
				super.update(entity);
			}
			info.setEntity(entity);
		}
		return info;
	}
	
	/**
	 * 节点跳转操作
	 */
	@Override
	public BpmMessage skipTask(OptionInfo optInfo,T entity,UserInfo user) {
		//用户校验
		if(proofRead!=null) {
			proofRead.ProofReadUser(user);
		}
		BpmMessage message= bpmFlowService.skipTask(optInfo, entity, user);
		super.update(entity);
		message.setEntity(entity);
		return message;
	}
	
	/**
	 * 项目退件
	 */
	@Override
	public BpmMessage disposal(OptionInfo optInfo,T entity,UserInfo user) {
		//用户校验
		if(proofRead!=null) {
			proofRead.ProofReadUser(user);
		}
		BpmMessage message= bpmFlowService.disposal(optInfo, entity, user);
		super.update(entity);
		message.setEntity(entity);
		return message;
	}
	
	/**
	 * 项目退件
	 */
	@Override
	public BpmMessage userDispose(OptionInfo optInfo,T entity,UserInfo user) {
		//用户校验
		if(proofRead!=null) {
			proofRead.ProofReadUser(user);
		}
		BpmMessage message= bpmFlowService.userDispose(optInfo, entity, user);
		super.update(entity);
		message.setEntity(entity);
		return message;
	}
	
	/**
	 * 项目退件
	 */
	@Override
	public BpmMessage easter(OptionInfo optInfo,T entity,UserInfo user) {
		//用户校验
		if(proofRead!=null) {
			proofRead.ProofReadUser(user);
		}
		BpmMessage message= bpmFlowService.easter(optInfo, entity, user);
		super.update(entity);
		message.setEntity(entity);
		return message;
	}
	
	/**
	 * 流程保存操作
	 * @param optionInfo 流程操作信息（包括用户、节点操作）
	 * @param entity
	 */
	public BpmMessage processSave(OptionInfo oInfo,T entity,UserInfo user) {
		//用户校验
		if(proofRead!=null) {
			proofRead.ProofReadUser(user);
		}
		BpmMessage message= bpmFlowService.processSave(oInfo, entity, user);
		String id=entity.getId();
		if(id!=null && !"".equals(id)) {
			//T entity=.getEntity(id);
			T entity2=super.getEntity(id);
			if(entity2!=null) {
				super.update(entity);
			}else {
				super.add(entity);
			}
		}
		message.setEntity(entity);
		return message;
	}
}
